/*
 * 创建日期 2005-12-29
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.logic;

import org.apache.commons.validator.GenericValidator;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.element.BeanElement;

/**
 * 逻辑处理标签父类 <br>
 * <p>
 * 
 * @author eagle <br>
 * @version Version 1.00 <br>
 */
public abstract class LogicElement extends BeanElement {
	/** 待比较的值 */
	private String value = null;
	
	/** 指定原值所在的位置，例如：A1（A列第1行） */
	private String property_cell = null;
	/** 指定待比较值所在的位置，例如：A1（A列第1行） */
	private String value_cell = null;


	private static int DOUBLE_COMPARE = 1;
	private static int LONG_COMPARE = 2;
	private static int STRING_COMPARE = 3;

	/**
	 * 标签执行方法，递归执行
	 * 计算汇率
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException {
		ReportInnerData reportData = (ReportInnerData) obj;
		String property = super.getProperty();

		try {
			//如果条件为true,才执行logic标签里内容
			if(!GenericValidator.isBlankOrNull(property)){
				if(condition(reportData)){
					super.execute(reportData);
				}
			}else{
				if(conditionCell(reportData)){
					super.execute(reportData);
				}
			}
		} catch (ReportException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReportException(e);
		}

	}
	
	protected abstract boolean condition(ReportInnerData reportData) throws Exception;
	protected abstract boolean conditionCell(ReportInnerData reportData) throws Exception;
	
	protected boolean condition(
		ReportInnerData reportData,
		int desired1,
		int desired2)
		throws Exception {


		//取得上下文Object
		Object contextObj = super.getNameObject(reportData);

		Object valueObj = null;
		try {
			//valueObj = super.getValue(value);
			if(!GenericValidator.isBlankOrNull(value_cell)){
				valueObj = ExcelUtils.getNumberCellValue(reportData.getReportSheet(),this.value_cell);
			}else{
				valueObj = value;
			}
			
		} catch (Exception e) {
			valueObj = "";
		}
		
		Object variable = null;
		try {
			//取得待比较的属性值
			variable = super.getProperty(contextObj, super.getProperty());
		} catch (Exception e) {
			variable = "";
		}
		
		return condition(variable,valueObj,desired1,desired2);
	}
	
	protected boolean conditionCell(
			ReportInnerData reportData,
			int desired1,
			int desired2)
			throws Exception {

		Object variable = ExcelUtils.getCellValue(reportData.getReportSheet(),this.property_cell);
		Object valueObj = null;
		if(!GenericValidator.isBlankOrNull(value_cell)){
			valueObj = ExcelUtils.getCellValue(reportData.getReportSheet(),this.value_cell);
		}else{
			valueObj = value;
		}

		return condition(variable,valueObj,desired1,desired2);
	}
	
	private boolean condition(Object variable,Object valueObj,int desired1,int desired2){
		int type = -1;
		double doubleValue = 0.0;
		long longValue = 0;
		
		if (type < 0) {
			try {
				doubleValue = Double.parseDouble(valueObj.toString());
				type = DOUBLE_COMPARE;
			} catch (NumberFormatException e) {
				;
			}
		}
		if (type < 0) {
			try {
				longValue = Long.parseLong(valueObj.toString());
				type = LONG_COMPARE;
			} catch (NumberFormatException e) {
				;
			}
		}
		if (type < 0) {
			type = STRING_COMPARE;
		}

		int result = 0;

		if (type == DOUBLE_COMPARE) {
			try {
				double doubleVariable = Double.parseDouble(variable.toString());
				if (doubleVariable < doubleValue)
					result = -1;
				else if (doubleVariable > doubleValue)
					result = +1;
			} catch (NumberFormatException e) {
				result = variable.toString().compareTo(valueObj.toString());
			}
		} else if (type == LONG_COMPARE) {
			try {
				long longVariable = Long.parseLong(variable.toString());
				if (longVariable < longValue)
					result = -1;
				else if (longVariable > longValue)
					result = +1;
			} catch (NumberFormatException e) {
				result = variable.toString().compareTo(valueObj.toString());
			}
		} else {
			result = variable.toString().compareTo(valueObj.toString());
		}

		// Normalize the result
		if (result < 0)
			result = -1;
		else if (result > 0)
			result = +1;

		// Return true if the result matches either desired value
		return ((result == desired1) || (result == desired2));

	}

	public String getValue() {
		return value;
	}

	public void setValue(String string) {
		value = string;
	}

	public String getProperty_cell() {
		return property_cell;
	}

	public void setProperty_cell(String property_cell) {
		this.property_cell = property_cell;
	}

	public String getValue_cell() {
		return value_cell;
	}

	public void setValue_cell(String value_cell) {
		this.value_cell = value_cell;
	}
}
